package com.zhang.controller;

import com.zhang.entity.Cart;
import com.zhang.entity.CartItem;
import com.zhang.entity.Product;
import com.zhang.service.ProductService;
import com.zhang.service.impl.ProductServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/cart")
public class CartController {
    private ProductService ps = new ProductServiceImpl();

    @RequestMapping("/putCart")
    public String addCart(int book_id, HttpSession session){
        Cart cart = (Cart)session.getAttribute("cart");
        if(cart == null){
            cart = new Cart();
        }
        if(cart.getRemoveMap().containsKey(book_id)){
            cart.getRemoveMap().remove(book_id);
        }
        boolean b = cart.getBuyMap().containsKey(book_id);
        if(b){
            CartItem cartItem = cart.getBuyMap().get(book_id);
            cartItem.setCount(cartItem.getCount()+1);
        }else{
            Product product = ps.selectById(book_id);
            cart.getBuyMap().put(book_id,new CartItem(product,1));
        }
        this.money(cart);
        session.setAttribute("cart",cart);
        return "redirect:/cart/cart_list.jsp";
    }

    @RequestMapping("/removeCart")
    public String removeCart(int book_id,HttpSession session){
        Cart cart = (Cart)session.getAttribute("cart");
        CartItem cartItem = cart.getBuyMap().get(book_id);
        cart.getBuyMap().remove(book_id);
        cart.getRemoveMap().put(book_id,cartItem);
        this.money(cart);
        return "redirect:/cart/cart_list.jsp";

    }

    @RequestMapping("/recoverCart")
    public String recoverCart(int book_id,HttpSession session){
        Cart cart = (Cart)session.getAttribute("cart");
        CartItem cartItem = cart.getRemoveMap().get(book_id);
        cart.getRemoveMap().remove(book_id);
        cart.getBuyMap().put(book_id,cartItem);
        this.money(cart);
        return "redirect:/cart/cart_list.jsp";
    }

    @RequestMapping("/changeNum")
    public String changeNum(int book_id,HttpSession session,int count){
        Cart cart = (Cart)session.getAttribute("cart");
        cart.getBuyMap().get(book_id).setCount(count);
        this.money(cart);
        return "redirect:/cart/cart_list.jsp";
    }


    private void money(Cart cart){
        double totalMoney = 0;
        double saveMoney = 0;
        Set<Map.Entry<Integer, CartItem>> entries = cart.getBuyMap().entrySet();
        for (Map.Entry<Integer,CartItem> entry:entries) {
            int count = entry.getValue().getCount();
            double book_price = entry.getValue().getProduct().getBook_price();
            double dangprice = entry.getValue().getProduct().getDangprice();
            totalMoney += dangprice*count;
            saveMoney += (book_price-dangprice)*count;
        }
        cart.setTotalMoney(totalMoney);
        cart.setSaveMoney(saveMoney);
    }
}
